package com.boang.state;

/*
  @author: 13813
  @date: 2024/11/27 9:52
*/
public class ThreadStateWaitDemo02 {
    public static void main(String[] args) {
        /*
            1.创建一个类实现 Runnalbe接口，重写run方法
              Abc implements Runnable {
                   run(){}
              }
              Abc abc = new Abc();
            2.new Thread(abc);

         */
        Object lock = new Object();//锁

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                synchronized (lock){
                    try {
                        lock.notify(); //唤醒 在此锁上 wait的线程
                        lock.wait(); //释放锁
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    System.out.println("abc");
                }
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (lock){
                    try {

                        lock.wait(); //释放锁
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    System.out.println("def");
                }
            }
        }).start();

        /*for (int i = 0; i < 1000; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        Thread.sleep(300000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    System.out.println("def");
                }
            }).start();
        }*/
    }
}
